package com.siteseat.crudeq.kernel;

import com.siteseat.crudeq.common.raw.Constants;
import com.siteseat.crudeq.common.raw.MessageHelper;

public class Message {

  private int   offset;

  private Chunk chunk;
  
  private int size;

  /**
   * @param chunkId
   * @param size
   * @param queueId
   */
  public Message( Chunk s, int offset, int size ) {
    this.chunk = s;
    this.offset = offset;
    assert size == MessageHelper.readSize( s.getPayload(), getRawOffset() );
    this.size = size;
  }

  /**
   * Gets the size
   *
   * @return the size
   */
  public int getSize() {
    return size;
  }

  /**
   * Gets the chunk
   * 
   * @return the chunk
   */
  public Chunk getChunk() {
    return chunk;
  }

  /**
   * @return
   */
  public int getRawOffset() {
    return this.offset;
  }
  
  public int getPayloadOffset() {
    return getRawOffset() + Constants.MESSAGE.OFFSET.PAYLOAD;
  }
  

  /**
   * 
   */
  public void commit() {
    getChunk().commit( this );
  }

  /**
   * 
   */
  public void rollback() {
    getChunk().rollback( this );
  }

}
